package com.bluexmicro.android.upgradebox.route

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.bluexmicro.android.upgradebox.screen.configure.ConfigVMFactory
import com.bluexmicro.android.upgradebox.screen.configure.ConfigureScreen
import com.bluexmicro.android.upgradebox.screen.configure.ConfigureViewModel
import com.bluexmicro.android.upgradebox.screen.file.FilePicker
import com.bluexmicro.android.upgradebox.screen.upgrade.UpgradeScreen

enum class Route(
    val key: String
) {
    CONFIGURE("Configure"),
    FILE_PICKER("FilePicker"),
    UPGRADE("Upgrade"),
}

@Composable
fun NavigationHost(
    navHostController: NavHostController = rememberNavController(),
) {
    val context = LocalContext.current
    val configureViewModel: ConfigureViewModel = viewModel(factory = ConfigVMFactory(context))

    LaunchedEffect(Unit) {
        configureViewModel.onLoad()
    }
    NavHost(
        navHostController,
        startDestination = Route.CONFIGURE.key,
    ) {
        composable(Route.CONFIGURE.key) {
            ConfigureScreen(
                configureViewModel,
                onPickFileListener = {
                    navHostController.navigate(Route.FILE_PICKER.key)
                },
                onUpgradeListener = {
                    navHostController.navigate(Route.UPGRADE.key)
                }
            )
        }
        composable(Route.FILE_PICKER.key) {
            FilePicker(onClose = {
                navHostController.popBackStack()
            }, onSelected = { file, address ->
                configureViewModel.onFileSelected(file, address)
                navHostController.popBackStack()
            })
        }
        composable(Route.UPGRADE.key) {
            UpgradeScreen {
                navHostController.popBackStack()
            }
        }
    }

}